package towerDefense.units;

import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.LinkedList;


@SuppressWarnings("serial")
public class PathingSprite extends ConstantMovementSprite
{

    private LinkedList<Point2D> myPath;


    public PathingSprite ()
    {
        super();
    }


    public PathingSprite (BufferedImage image,
                          double x,
                          double y,
                          double r,
                          double spd,
                          LinkedList<Point2D> path)
    {
        super(image, x, y, r, spd);
        setPath(path);
    }


    public PathingSprite (BufferedImage image,
                          double r,
                          double spd,
                          LinkedList<Point2D> path)
    {
        super(image, r, spd);
        setPath(path);
    }


    public PathingSprite (double x, double y, double r, LinkedList<Point2D> path)
    {
        super(x, y, r);
        setPath(path);
    }


    public void setPath (LinkedList<Point2D> path)
    {
        this.myPath = path;
    }


    public LinkedList<Point2D> getPath ()
    {
        return myPath;
    }


    public void addPoint (Point2D p)
    {
        myPath.add(p);
    }


    public void clearPath ()
    {
        myPath.clear();
    }


    @Override
    public void update (long elapsedTime)
    {
        super.update(elapsedTime);

        if (!myPath.isEmpty())
        {
            if (this.moveTo(elapsedTime,
                            myPath.getFirst().getX() - this.getWidth() / 2,
                            myPath.getFirst().getY() - this.getHeight() / 2,
                            this.getConstantSpeed()))
            {
                myPath.removeFirst();

            }
        }
        else if (this.isOnScreen()) this.setMovement(0, 0);
        else if (this.isActive()) doOffScreenAction();

    }


    protected void doOffScreenAction ()
    {

        this.setActive(false);
    }

}
